import json
import os

from flask import Blueprint, request, jsonify

from db import db1

bp_course = Blueprint("course", __name__)


@bp_course.route('/courseM', methods=['POST', 'GET', 'PUT', 'DELETE'])
def couponM():
    # 添加
    if request.method == "POST":
        # 获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        # 构造sql语句
        sql = "insert into coupon values(0,'%s',%d,'%s','%s')" % (
            data['name'], int(data['total']), data['stime'], data['etime'])
        # pymysql
        db1.insert(sql)
        # 返回结果
        return 'ok'
    elif request.method == 'GET':
        id = request.args.get('id')
        sql = "select * from coupon where id=%d" % (int(id))
        res = db1.find(sql)
        return jsonify({'code': 200, 'coupon': res})
    elif request.method == 'PUT':
        # 获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "update coupon set name='%s',total=%d,startime='%s',endtime='%s' where id=%d" % (
            data['name'], int(data['total']), data['stime'], data['etime'], int(data['id']))
        db1.insert(sql)
        return jsonify({'code': 200})
    elif request.method == 'DELETE':
        id = request.args.get('id')
        sql = "delete from coupon where id=%d" % (int(id))
        db1.insert(sql)
        return jsonify({'code': 200})


from utils.myfile import myfile


# 文件上传
@bp_course.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    myfile.file_mkdir(2)
    file.save(os.path.join('./static/upload/2', file.filename))
    return 'ok'


@bp_course.route('/city', methods=['POST', 'GET'])
def city():
    if request.method == "POST":
        data = request.form
        sql = "insert into city values (0,'%s')" % (data['name'])
        db1.insert(sql)
        return jsonify({'code': 200})

    elif request.method == "GET":
        sql = "select * from city"
        res = db1.findall(sql)
        return jsonify({'code': 200, 'list': res})


@bp_course.route('/product', methods=['POST', 'GET'])
def product():
    if request.method == 'POST':
        city_id = request.data['cid']
        file = request.files['file']
        myfile.file_mkdir(city_id)
        file.save(os.path.join('./static/upload/' + city_id, file.filename))
        return 'ok'
    else:
        cityid = request.args.get('cityid')
        if cityid == 0:
            sql = "select * from product"
        else:
            sql = "select * from product where city_id=%d" % (int(cityid))

        res = db1.findall(sql)
        return jsonify({"code": 200, "list": res})

        # @bp_course.route('/page',methods=['GET'])
        # def page():
        sort = request.args.get('sort')
        if cityid == '0':
            sql = "select * from product order by price %s limit %d, %d" % (sort, start, page_size)
            sql1 = "select count(id) from product"
            res = db1.find(sql1)
            total = res['count(id)']
        else:
            sql2 = "select * from product where city_id=%d order by price %s limit %d,%d" % (int(cityid), sort, start)
            db1.findall(sql2)
